#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../include/cmd_trie.h"

int trie_insert(trie_t *trie,const char *cmd,cmd_func_ptr_t func){
    trie_t *p=trie;
    for(int i=0;cmd[i]!='\0';i++){
        if(!isalpha(cmd[i])){
            return -1;
        }
        int c=tolower(cmd[i])-'a';
        if(p->children[c]==NULL){
            p->children[c]=(trie_t*)calloc(1,sizeof(trie_t));
        }
        p=p->children[c];
    }
    p->func=func;
    return 0;
}

cmd_func_ptr_t trie_get_func(trie_t *trie,const char *cmd){
    trie_t *p=trie;
    for(int i=0;cmd[i]!='\0';i++){
        if(!isalpha(cmd[i])){
            return NULL;
        }
        int c=tolower(cmd[i])-'a';
        if(p->children[c]==NULL){
            return NULL;
        }
        p=p->children[c];
    }
    return p->func;
}